home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Prog / D-G / FB_AppleEvents.cpt / AEVENTS.MAIN < prev    next >
Text File  |  1992-05-03  |  9KB  |  270 lines

  1. '*************************************************
  2. '***************** Apple Events ******************
  3. '*************************************************
  4. '* by George Ritchie (AO: GRITCHIE) May 2, 1992 *
  5. '*************************************************
  6. '* written in FUTURE BASIC™ ( © Zedcor, Inc. 1992) *
  7. '*************************************************
  8. '* This program implements the four
  9. '* Apple Events (open application, open document,
  10. '* print document, quit) required of "System-7
  11. '* friendly" applications. A one-line text
  12. '* document (AEvents.doc) with the appropriate creator
  13. '* type is included with this program. Send the
  14. '* application an "Open" or "Print" event for this
  15. '* document from the Finder - the document will be
  16. '* opened and its contents displayed in the
  17. '* application's window.
  18. '*************************************************
  19. '* The "SIZE -1" resource int the AEVENTS.RSRC
  20. '* file has the "High Level Event Aware" flag
  21. '* set - this is required for getting the Finder
  22. '* to send the Apple Events to the application.
  23. '*************************************************
  24. '* The "include" file contains assembly-language
  25. '* versions of the system function calls required
  26. '* for handling Apple Events and opening files
  27. '* using System-7 specific routines. As of this
  28. '* writing, these calls are not part of the
  29. '* standard Future Basic library.
  30. '*************************************************
  31. '* You must BUILD the application and run it
  32. '* separately from Future Basic. (Otherwise you won't
  33. '* be able to receive Apple Events!)
  34. '*************************************************
  35. '* Please don't even think about running this
  36. '* without System 7! A real program must use Gestalt
  37. '* to check for the existence of the PPC toolbox
  38. '* (standard part of Sys. 7) before using Apple Events.
  39. '*************************************************
  40. '* I want to extend this program example so that
  41. '* it can send and receive other types of Apple Events
  42. '* (including custom-defined ones) that will execute
  43. '* menu commands, button pushes, send data etc.
  44. '* The objective is to produce applications that are
  45. '* scriptable using the Apple-Event protocol.
  46. '* Any suggestions or advice from others on using
  47. '* Apple Events for scripting would be most welcome!
  48. '*************************************************
  49. '* credits:
  50. '* The FUTURE BASIC compiler was created by Andrew Gariepy. *
  51. '* Information on Apple Events was obtained from:
  52. '* Inside Macintosh Vol. 6 (Apple, 1991)
  53. '* Programming for System 7 (Addison Wesley, 1991)
  54. '* Macintosh Programmers Workshop 3.2 (Apple, 1991)
  55. '**************************************************
  56.  
  57. DEFSTR LONG
  58. RESOURCES "AEVENTS.RSRC","TEXTpAr3"
  59. INCLUDE "AEVENTS.INCL"
  60.  
  61. _nil=0
  62. _LocationNameRecLen=136
  63. _PortInfoRecLen=82
  64. _PPCPortRecLen=80
  65. _ioMisc=28
  66.  
  67. gQuit=0
  68.  
  69. '* class and types of events we will handle *
  70. '* all required evnents are members of the Core Event class *
  71. kCoreEventClass&=CVI("aevt")
  72. kAEOpenApplication&=CVI("oapp")
  73. kAEOpenDocuments&=CVI("odoc")
  74. kAEQuitApplication&=CVI("quit")
  75. kAEPrintDocuments&=CVI("pdoc")
  76.  
  77. '* parameters are passed directly in these events *
  78. keyDirectObject&=CVI("----")
  79.  
  80. '* define data-structure types that will be used *
  81. typeAEList&=CVI("list")
  82. typeAERecord&=CVI("reco")
  83. typeFSS&=CVI("fss ")
  84.  
  85. ' * tagged data, the standard AppleEvent data type *
  86. DIM RECORD AEDesc
  87.   DIM AEdescriptorType&:                  '* data type specification *
  88.   DIM AEdataHandle&:                      '* handle to the data *
  89. DIM END RECORD _AEDescRecLen
  90.  
  91. '* other data structures that we need: *
  92.  
  93. DIM PPCPortRec. _PPCPortRecLen
  94. DIM theLocation. _LocationNameRecLen
  95. DIM thePortInfo. _PortInfoRecLen
  96. DIM theAEDesc. _AEDescRecLen
  97.  
  98. DIM RECORD TargetID
  99.   DIM sessionID&
  100.   DIM TargetIDname. _PPCPortRecLen
  101.   DIM TargetIDlocation. _LocationNameRecLen
  102.   DIM TargetIDrecvrName. _PPCPortRecLen
  103. DIM END RECORD
  104.  
  105. DIM RECORD gTheAddress
  106.   DIM gTheAdddescriptorType&
  107.   DIM gTheAdddataHandle&
  108. DIM END RECORD
  109.  
  110. GOTO "START":                             '* skip over functions *
  111.  
  112. LONG FN GetAEDocumentInfo(AE_messageinPTR&,PrintDoc)
  113.                                           '* extract the required info from Apple Event to open a document
  114.   DIM itemsInList&,index&,keywd&,returnedType&,actualSize&
  115.   
  116.                                           '* System-7 file info structure: *
  117.   DIM RECORD myFSS
  118.     DIM myFSSvRefNum%
  119.     DIM myFSSparID&
  120.     DIM 63 myFSSname$
  121.   DIM END RECORD _myFSSRecLen
  122.   
  123.                                           '* list of documents to be opened: *
  124.   DIM docList. _AEDescRecLen
  125.   
  126.   myErr=FN AEGetParamDesc(AE_messageinPTR&,keyDirectObject&,typeAEList&,@docList)
  127.   
  128.   LONG IF myErr
  129.     BEEP
  130.   XELSE
  131.                                           '* see if all parameters required for this Apple Event are actually there *
  132.     myErr=FN MyGotRequiredParams(AE_messageinPTR&)
  133.   END IF
  134.   
  135.   LONG IF myErr=0
  136.     
  137.                                           '* how many document were requested to be opened? *
  138.     myErr=FN AECountItems(@docList,@itemsInList&)
  139.     
  140.     LONG IF myErr
  141.       BEEP
  142.     XELSE
  143.       PRINT "ITEMS IN LIST=";itemsInList&
  144.       FOR index&=1 TO itemsInList&
  145.                                           '* extract info on a particular document *
  146.         myErr=FN AEGetNthPtr(@docList,index&,typeFSS&,@keywd&,@returnedType&,@myFSS,_myFSSRecLen,@actualSize&)
  147.         LONG IF myErr
  148.           BEEP
  149.         XELSE
  150.                                           '* vRefNum% is volume no. - it does not identify a folder *
  151.           PRINT "vRefNum%=";myFSSvRefNum%;", FILENAME=";myFSSname$
  152.           
  153.                                           '* Following function opens our document, given
  154.                                           '* the FSS record for it.
  155.           myErr=FN OpenDoc(@myFSS,@toDataHandl&)
  156.           
  157.           LONG IF toDataHandl&
  158.                                           '* we opened and red the file, so show its contents *
  159.             A$=""
  160.             L=FN GETHANDLESIZE(toDataHandl&)
  161.             IF L>32 THEN L=32
  162.             BLOCKMOVE PEEK LONG(toDataHandl&),@A$+1,L
  163.             POKE @A$,L
  164.             PRINT A$
  165.             IF PrintDoc THEN PRINT "CALL PRINT ROUTINE HERE"
  166.             X=FN DISPOSHANDLE(toDataHandl&)
  167.           END IF
  168.           
  169.         END IF
  170.         
  171.       NEXT index&
  172.       
  173.     END IF
  174.   END IF
  175. END FN
  176.  
  177. LONG FN InitAEStuff
  178.   aevtErr = FN AEInstallEventHandler(kCoreEventClass&,kAEOpenApplication&,LINE "AEOpenHandler",0,_false)
  179.   aevtErr = FN AEInstallEventHandler(kCoreEventClass&,kAEOpenDocuments&,LINE "AEOpenDocHandler",1,_false)
  180.   aevtErr = FN AEInstallEventHandler(kCoreEventClass&,kAEQuitApplication&,LINE "AEQuitHandler",2,_false)
  181.   aevtErr = FN AEInstallEventHandler(kCoreEventClass&,kAEPrintDocuments&,LINE "AEPrintHandler",3,_false)
  182. END FN
  183.  
  184. LOCAL FN DoHighLevel(AERecordPtr&)
  185.   FN AEProcessAppleEvent(AERecordPtr&)
  186. END FN
  187.  
  188. "START"
  189. WINDOW 1
  190.  
  191. '* install the Apple Event handlers *
  192. FN InitAEStuff
  193.  
  194. ON EVENT GOSUB "EVENT"
  195. ON DIALOG GOSUB "DIALOG"
  196. ON BREAK GOSUB "REMOVE_EVENT_HANDLERS"
  197.  
  198. DO
  199.   EVENT ON:DIALOG ON:BREAK ON
  200.   EVENT OFF:DIALOG OFF:BREAK OFF
  201. UNTIL gQuit
  202.  
  203. GOSUB "REMOVE_EVENT_HANDLERS"
  204.  
  205. END
  206.  
  207.  
  208. "REMOVE_EVENT_HANDLERS"
  209. '* before quitting, remove Apple Event handlers we had installed *
  210. aevtErr = FN AERemoveEventHandler(kCoreEventClass&,kAEOpenApplication&,AEOpenHandler&,_false)
  211. aevtErr = FN AERemoveEventHandler(kCoreEventClass&,kAEOpenDocuments&,AEOpenDocHandler&,_false)
  212. aevtErr = FN AERemoveEventHandler(kCoreEventClass&,kAEQuitApplication&,AEQuitHandler&,_false)
  213. aevtErr = FN AERemoveEventHandler(kCoreEventClass&,kAEPrintDocuments&,AEPrintHandler&,_false)
  214. gQuit=-1
  215. RETURN
  216.  
  217. "EVENT"
  218. '* check for high-level event before letting Future Basic handle the event *
  219. LONG IF PEEK WORD(EVENT)=_kHighLevelEvent
  220.   FN DoHighLevel(EVENT)
  221.   POKE WORD (EVENT),0:                    '* clear the event, we handled it *
  222.   CURSOR 0
  223. END IF
  224. RETURN
  225.  
  226. "DIALOG"
  227. '* get low-level event information *
  228. DIALOG_ZERO=DIALOG(0)
  229. DIALOG_ONE=DIALOG(DZERO)
  230. RETURN
  231.  
  232. '* The following PROCs automatically take care of setting up
  233. '* and restoring CPU registers:
  234.  
  235. "AEOpenHandler"
  236. '*  Open Application event.*
  237. ENTERPROC (AE_messageinPTR&,AE_replyPTR&,refIn&)
  238.   AE_replyPTR&=0:                         '* no reply to this event *
  239.   PRINT "open appl EVENT"
  240.   returnMessage%=_noErr
  241. EXITPROC=returnMessage%
  242.  
  243. "AEOpenDocHandler"
  244. '*  Open Document event.*
  245. ENTERPROC (AE_messageinPTR&,AE_replyPTR&,refIn&)
  246.   AE_replyPTR&=0
  247.   FN GetAEDocumentInfo(AE_messageinPTR&,0)
  248. EXITPROC=myErr
  249.  
  250. "AEPrintHandler"
  251. '* Print Document event.*
  252. ENTERPROC (AE_messageinPTR&,AE_replyPTR&,refIn&)
  253.   AE_replyPTR&=0
  254.   FN GetAEDocumentInfo(AE_messageinPTR&,1)
  255.   gPrintDoc=0
  256. EXITPROC=myErr
  257.  
  258.  
  259. "AEQuitHandler"
  260. '* Standard Quit event handler, to handle a Quit event from the Finder, for example.  *
  261. ENTERPROC (AE_messageinPTR&,AE_replyPTR&,refIn&)
  262.   AE_replyPTR&=0
  263.   PRINT "quit EVENT"
  264.   DELAY 1000
  265.                                           '* never quit here - return to main loop before quitting! *
  266.   gQuit = _true
  267.   returnMessage%= _noErr
  268. EXITPROC=returnMessage%
  269.  
  270.